package svl.casacentral.delegate;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import ejb.casacentral.log.CasaLogger;

public class SessionDelegate {
	
	@SuppressWarnings("unchecked")
	public static <T> T obtenerBean(Class<T> object) {
		
		String provider_url = "jnp://127.0.0.1:1099";
		Properties properties = new Properties();
		Context ctx;
		properties.put(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
		properties.put(InitialContext.PROVIDER_URL, provider_url);
		
		T sessionbean;

		try {
			ctx = new InitialContext(properties);

			CasaLogger.log("CasaEar/"+object.getSimpleName()+"/local");

			sessionbean =  (T) ctx.lookup("CasaEar/"+object.getSimpleName()+"/local");
			
			return (T) sessionbean;

		} catch (NamingException e) {
			e.printStackTrace();
			return null;
		}

	}

}
